<?php
namespace plugins\goods\controller;

use cmf\controller\PluginRestBaseController; //引用插件基类
use plugins\goods\model\GoodsCommentModel;
use think\Db;

class ApiCommonCommentController extends PluginRestBaseController
{
    protected $uid = '';
    protected $model;

    /**
     * 初始化
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new GoodsCommentModel;
        // $this->uid   = $this->getIdentity()->uid;
    }
    /**
     * 默认头像
     */
//    private $head = 'plugins' . DS . 'goods' . DS . 'view' . DS . 'public' . DS . 'image' . DS . 'head.png';
    /**
     * 评论列表
     */
    public function commentList()
    {
        $param = $this->request->post();
        checkParam(['gid', 'pageNum', 'page', 'type'], $param);
        //1好评率2好评，中评，差评搜索加数量 3评论总数，4.评论第一条好评
        $where = "goods_id=" . $param['gid'];
        $order = "sort asc,add_time desc";
        // if (!empty($param['sort']) && $param['sort'] = 2) {
        //     $order = "add_time desc";
        // }
        $type_num = [
            'all_num'   => 0, //全部数量
            'very_good' => 0, //好评数
            'general'   => 0, //中评数
            'rubbish'   => 0, //差评数
        ];
        $search = 1;
        //评论类型
        switch ($param['type']) {
            case 'all': //全部评论
                break;
            case '1': //好评
                $search .= " and score in (4,5)";
                break;
            case '2': //中评
                $search .= " and score = 3";
                break;
            case '3': //差评
                $search .= " and score in (1,2)";
                break;
            default:
                return api_json_echo(false, '类型错误', null, 101);
                break;
        }
        $type_num['very_good'] = $this->model->where($where)->where('score', 'in', [4, 5])->count();
        $type_num['general']   = $this->model->where($where)->where('score', 3)->count();
        $type_num['rubbish']   = $this->model->where($where)->where('score', 'in', [1, 2])->count();
        $type_num['all_num']   = $type_num['very_good'] + $type_num['general'] + $type_num['rubbish'];
        //好评率
        @$goods_ratio = $type_num['very_good'] / $type_num['all_num'];
        $goods_ratio  = ((number_format($goods_ratio, 2)) * 100) . "%"; //保留两位小数

        $data = $this->model
            ->where($where)->where($search)->order($order)
            ->field('id,uid,score,comment,img,goods_img,add_time,nick_name,head_portrait,goods_spec,reply,is_anonym')
            ->paginate($param['pageNum'], false, ['page' => $param['page']])
            ->each(function ($item) {
                $trans                 = $this->transform($item);
                $item['img']           = $trans['img'];
                $item['add_time']      = $trans['add_time'];
                $item['goods_img']     = $trans['goods_img'];
                $item['nick_name']     = $trans['nick_name'];
                $item['head_portrait'] = $trans['head_portrait'];
                //查询店家回复
                $reply = db('goods_comment_reply')->where(['cid' => $item['id'], 'reply_type' => 2])->field('comment,add_time')->select();
                if ($reply) {
                    $item['store_reply'] = $reply;
                }
                return $item;
            })->toArray();
        $data['type_num']    = $type_num; //商品类型数量
        $data['goods_ratio'] = $goods_ratio; //好评率
        return api_json_echo(true, '成功', $data, 200);
    }

    /**
     * 获取最新好评
     */
    public function getCommentFirst()
    {
        $param = $this->request->post();
        checkParam(['gid'], $param);
        $where = "goods_id=" . $param['gid'];
        //按照评分排序
        $list     = $this->model->where($where)->order('add_time', 'desc')->order('score', 'desc')->find();
        $type_num = [
            'all_num'   => 0, //全部数量
            'very_good' => 0, //好评数
            'general'   => 0, //中评数
            'rubbish'   => 0, //差评数
        ];
        $type_num['very_good'] = $this->model->where($where)->where('score', 'in', [4, 5])->count();
        $type_num['general']   = $this->model->where($where)->where('score', 3)->count();
        $type_num['rubbish']   = $this->model->where($where)->where('score', 'in', [1, 2])->count();
        $type_num['all_num']   = $type_num['very_good'] + $type_num['general'] + $type_num['rubbish'];
        //好评率
        @$goods_ratio = $type_num['very_good'] / $type_num['all_num'];
        $goods_ratio  = ((number_format($goods_ratio, 2)) * 100) . "%"; //保留两位小数

        try {
            $trans                 = $this->transform($list);
            $list['img']           = $trans['img'];
            $list['add_time']      = $trans['add_time'];
            $list['goods_img']     = $trans['goods_img'];
            $list['nick_name']     = $trans['nick_name'];
            $list['head_portrait'] = $trans['head_portrait'];
            //查询店家回复
            $reply = db('goods_comment_reply')->where(['cid' => $list['id'], 'reply_type' => 2])->field('comment,add_time')->select();
            if ($reply) {
                $list['store_reply'] = $reply;
            }
        } catch (\Exception $e) {

        }

        $list['type_num']    = $type_num; //商品类型数量
        $list['goods_ratio'] = $goods_ratio; //好评率
        return api_json_echo(true, '成功', $list, 200);
    }

    /**
     * 返回值转化
     */
    private function transform($data)
    {
        //图片转化
        $img = json_decode($data['img']) ?: [];
        foreach ($img as $im_k => $im_v) {
            $im_v       = zy_domain() . $im_v;
            $img[$im_k] = $im_v;
        }
        $data['img'] = $img;
        //时间转化
        $data['add_time'] = date('Y-m-d H:i:s', $data['add_time']);
        //商品图转化
        if (!empty($data['goods_img'])) {
            $data['goods_img'] = zy_domain() . '/plugins/goods/view/image/' . $data['goods_img'];
        }

        //判断是否匿名
        if ($data['is_anonym'] == 1) {
            $anon                  = new GoodsCommentModel();
            $data['nick_name']     = $anon->anonym($data['nick_name']);
            $data['head_portrait'] = zy_domain() . '/' . $this->head; //默认头像

        } else {
            $data['head_portrait'] = zy_domain() . $data['head_portrait']; //默认头像
        }
        return $data;
    }

    /**
     * 查看评论
     * @param  $id        评论id
     */
    public function commentDetails()
    {
        $param = $this->request->post();
        if (empty($param['id'])) {
            return api_json_echo(false, '未传id', '', 100);
        }
        $data                  = db('goods_comment')->where('id', $param['id'])->find();
        $trans                 = $this->transform($data);
        $data['img']           = $trans['img'];
        $data['add_time']      = $trans['add_time'];
        $data['goods_img']     = $trans['goods_img'];
        $data['nick_name']     = $trans['nick_name'];
        $data['head_portrait'] = $trans['head_portrait'];
        $data['reply_list']    = db('goods_comment_reply')->where('cid', $param['id'])->select();
        return api_json_echo(true, '成功', $data, 200);
    }



}
